﻿using System;
using Shell.DomainDot;
using Framework.Poco;

namespace Framework.Domain
{
    /// <summary>
    /// 经销商文件
    /// </summary>
    public class AgencyBinaryFileContext : DomainDotBase<Agency>
    {
        /// <summary>
        /// 属于经销商的文件
        /// </summary>
        public WithOne<BinaryFile> BinaryFileBelonged { private get; set; }

        /// <summary>
        /// 获取属于经销商的文件
        /// </summary>
        public BinaryFile GetBinaryFile()
        {
            return this.BinaryFileBelonged.Instance;
        }

        /// <summary>
        /// 获取属于经销商的文件ID
        /// </summary>
        public Guid GetBinaryFileIdentity()
        {
            if (this.BinaryFileBelonged.Instance != null)
            {
                return this.BinaryFileBelonged.Instance.Id;
            }
            return Guid.Empty;
        }

        /// <summary>
        /// 绑定文件
        /// </summary>
        public void BindBinaryFile(Guid identity)
        {
            this.BinaryFileBelonged.Bind(identity);
            this.BinaryFileBelonged.Submit();
        }

        /// <summary>
        /// 修改图片
        /// </summary>
        public void EditBinaryFile(Guid identity)
        {
            this.BinaryFileBelonged.Delete();
            this.BinaryFileBelonged.Submit();
            this.BindBinaryFile(identity);
        }

        /// <summary>
        /// 删除经销商LOGO
        /// </summary>
        public bool Delete(Guid identity)
        {
            var logo = this.GetBinaryFile();
            if (logo.Id.Equals(identity))
            {
                this.BinaryFileBelonged.Delete();
                this.BinaryFileBelonged.Submit();
                return true;
            }
            return false;
        }
    }
}
